package org.projectwithoutname.ui.wizard;

import java.util.Date;

import model.Episode;
import model.EpisodesPackEpisode;
import model.EpisodesPackage;
import model.Season;
import model.SeasonPackage;
import model.SeasonsPackSeason;
import model.Serie;
import model.SeriesPackSerie;
import model.Workspace;
import model.impl.ModelFactoryImpl;

import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.projectwithoutname.ui.wizard.pages.EpisodesPage;

public class NewEpisodeWizard extends Wizard implements INewWizard {
	IWorkbench workbench;
	IStructuredSelection selection;
	private EpisodesPage episodesPage;
	private Workspace root;
	private Serie serie;
	private String[] seasons;
	private Season season;
	
	public NewEpisodeWizard(Serie serie, Workspace root){
		this.root = root;
		this.serie = serie;
		EList<SeasonsPackSeason> seasonPackage = serie.getAllSeason().getAggregatedSeason();
		seasons = new String[seasonPackage.size()];
		int i = 0;
		for (SeasonsPackSeason packSeason : seasonPackage){
			seasons[i] = packSeason.getSeason().getName();
			i++;
		}
	}
	
	
	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.workbench = workbench;
		this.selection = selection;
	}
	
	@Override
	public void addPages() {
		episodesPage = new EpisodesPage("New Episode", seasons);
		addPage(episodesPage);
	}

	
	@Override
	public boolean performFinish() {

//		get a season
		SeasonPackage seasonPackage = serie.getAllSeason();
		for (SeasonsPackSeason seasonLink : seasonPackage.getAggregatedSeason()){
			season = seasonLink.getSeason();
			if (season.getName().equals(episodesPage.getChooseSeason()))
				break;
		}
		
//		create a episodes package with doesn't have one
		
		EpisodesPackage episodesPackage = null;
		if (season != null){
			if (season.getGroupOfEpisodes() == null){
				episodesPackage = ModelFactoryImpl.eINSTANCE.createEpisodesPackage();
			}
			else
				episodesPackage = season.getGroupOfEpisodes();
		}
		
//		create a new episode
		EpisodesPackEpisode episodeList = ModelFactoryImpl.eINSTANCE.createEpisodesPackEpisode();
		Episode episode = ModelFactoryImpl.eINSTANCE.createEpisode();
		episode.setPath(episodesPage.path());
		episode.setName(episodesPage.getName());
		episode.setDescription(episodesPage.getDescription());
		
		episodeList.setEpisode(episode);
		episodesPackage.getEpisodeComponent().add(episodeList);
		season.setGroupOfEpisodes(episodesPackage);
		root.getEpisodesPackage().add(episodesPackage);
		root.getEpisodes().add(episode);
		
		return true;
	}
	
	@SuppressWarnings("deprecation")
	public Date convert(DateTime date){
		Date d = new Date();
		d.setDate(date.getDay());
		d.setMonth(date.getMonth());
		d.setYear(date.getYear());
		return d;
	}

}
